package Crm;

import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.TestOnly;

import java.util.ArrayList;
import java.util.Objects;
import java.util.Scanner;

public class util {

    static ArrayList<User> userArrayList = new ArrayList<>();
    static Scanner scan = new Scanner(System.in);

    public static void menu() {
        System.out.println("**************客户信息管理软件**************");
        System.out.println("               1.客户列表");
        System.out.println("               2.增加客户");
        System.out.println("               3.修改信息");
        System.out.println("               4.删除客户");
        System.out.println("               5.退   出");
        System.out.print("                 请输入1-5:");
    }

    public static void showUserList() {
        System.out.println("--------------客户列表--------------");
        System.out.println("id\t姓名\t性别\t年龄\t电话\t邮箱");
        if (userArrayList.size() > 0) {
            for (int i = 0; i < userArrayList.size(); i++) {
                User user = userArrayList.get(i);
                System.out.printf("%d\t%s\t%s\t%d\t%s\t%s\n", i+1, user.getName(), user.getSex(), user.getAge(), user.getPhone(), user.getMail());
            }
        }
    }

    public static char readFlag() {
        return scan.next().charAt(0);
    }

    public static char confirm() {
        return scan.next().charAt(0);
    }

    public static void addUser() {
        User tempUser = new User();
        System.out.print("请输入用户姓名:");
        tempUser.setName(scan.next());
        System.out.print("请输入用户性别:");
        tempUser.setSex(scan.next());
        System.out.print("请输入用户年龄:");
        tempUser.setAge(scan.nextByte());
        System.out.print("请输入用户电话:");
        tempUser.setPhone(scan.next());
        System.out.print("请输入用户邮箱:");
        tempUser.setMail(scan.next());
        showUser(tempUser);
        System.out.print("是否确定添加(y/n):");
        char flag = confirm();
        if (flag == 'y') {
            userArrayList.add(tempUser);
        }
    }

    private static @NotNull String nextLinToString() {
        String tempString = scan.nextLine();
        if (!Objects.equals(tempString, "")) {
            return tempString.trim();
        }
        return "";
    }

    private static byte nextLinToByte() {
        String tempString = scan.nextLine();
        if (!Objects.equals(tempString, "")) {
            return Byte.parseByte(tempString.trim());
        }
        return -1;
    }

    public static void modifyUser() {
        System.out.print("请输入要修改用户的id:");
        int userId = scan.nextInt();
        User tempUser = userArrayList.get(userId-1);
        System.out.println("不需要修改请直接回车...");
        System.out.printf("请输入新的用户姓名(%s):", tempUser.getName());
        tempUser.setName(nextLinToString());
        System.out.printf("请输入新的用户性别(%s):", tempUser.getSex());
        tempUser.setSex(nextLinToString());
        System.out.printf("请输入新的用户年龄(%d):", tempUser.getAge());
        tempUser.setAge(nextLinToByte());
        System.out.printf("请输入新的用户电话(%s):", tempUser.getPhone());
        tempUser.setPhone(nextLinToString());
        System.out.printf("请输入新的用户邮箱(%s):", tempUser.getMail());
        tempUser.setMail(nextLinToString());
    }

    public static void deleteUser() {
        System.out.print("请输入要删除用户的id:");
        int userId = scan.nextInt();
        System.out.printf("是否确认要删除id为%d的用户(y/n):", userId);
        char flag = confirm();
        if (flag == 'y') {
            userArrayList.remove(userId - 1);
        } else {
            System.out.println("未执行删除操作...");
        }
    }

    private static void showUser(@NotNull User user) {
        System.out.printf("%s\t%s\t%d\t%s\t%s\n", user.getName(), user.getSex(), user.getAge(), user.getPhone(), user.getMail());
    }

}
